/*
* CourseRegisterController.java
* Created on  2014-5-8 下午2:32
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-5-8       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.courseregister;

import com.genghis.core.autocomplete.service.AutoCompleteService;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.classify.entity.Type;
import com.genghis.ptas.classify.entity.TypeValues;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.course.entity.ChapterRegister;
import com.genghis.ptas.course.entity.CourseCollect;
import com.genghis.ptas.course.entity.CourseRegister;
import com.genghis.ptas.course.service.CourseCollectService;
import com.genghis.ptas.course.service.CourseRegisterService;
import com.genghis.ptas.course.service.CourseService;
import com.genghis.ptas.course.service.CourseStatisticsService;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;

/**
 * 类的描述信息
 *
 * @author fengxuanzhen
 * @version 1.0.1
 */
@Controller
@Transactional
@RequestMapping("/courseRegister/")
public class CourseRegisterController {

    @Autowired
    private CourseRegisterService courseRegisterService;

    @Autowired
    private CourseCollectService courseCollectService;

    @Autowired
    private CourseStatisticsService courseStatisticsService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private UserService userService;

    @Autowired
    private AutoCompleteService autoCompleteService;

    @RequestMapping(value = "listAllCourseRegisterDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String listAllCourseRegisterDo() {
        return "backstage/course/register/listAllCourseRegisterDo";
    }

    @RequestMapping(value = "listAllCourseRegister", method = RequestMethod.POST)
    @ResponseBody
    public Page<CourseRegister> listAllCourseRegister(PageParameter pageParameter, CourseRegister courseRegister) {
        return courseRegisterService.findCourseRegisterPageList(pageParameter, courseRegister);
    }

    @RequestMapping(value = "editCourseRegisterDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String editCourseRegisterDo(HttpServletRequest request, @RequestParam("id") Integer id) throws GeneralSecurityException {
        CourseRegister courseRegister = courseRegisterService.findCourseRegisterById(id);
        request.setAttribute("courseRegister", courseRegister);
        return "backstage/course/register/editCourseRegisterDo";
    }

    @RequestMapping(value = "addCourseRegister", method = RequestMethod.POST)
    @ResponseBody
    public Boolean addCourseRegister(CourseRegister courseRegister) throws ServletException, IOException {
        Boolean judge = courseRegisterService.checkWhetherRegisterByCourseId(courseRegister.getCourseId());
        if (judge) {
            courseRegisterService.addCourseRegister(courseRegister);
        }
        return judge;
    }

    @RequestMapping(value = "courseWhetherRegister", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Boolean courseWhetherRegister(CourseRegister courseRegister) throws ServletException, IOException {
        return courseRegisterService.checkWhetherRegisterByCourseId(courseRegister.getCourseId());
    }

    @RequestMapping(value = "collectionCourse", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Boolean collectionCourse(CourseCollect courseCollect) throws ServletException, IOException {
        Boolean judge = courseCollectService.judgeWhetherCollectByCourseId(courseCollect);
        if (judge) {
            courseCollectService.addCourseCollectService(courseCollect);
        }
        return judge;
    }

    @RequestMapping(value = "submitScore", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public void submitScore(CourseRegister courseRegister) throws ServletException, IOException {
        CourseRegister courseRegister1 = courseRegisterService.findCourseRegisterByCourseIdAndUserId(courseRegister.getCourseId()).get(0);
        courseRegister1.setScore(courseRegister.getScore());
        courseRegisterService.editCourseRegister(courseRegister1);
        courseStatisticsService.updateAverageScoreByCourseId(courseRegister.getCourseId());
    }

    @RequestMapping(value = "editCourseRegister", method = RequestMethod.POST)
    @ResponseBody
    public void editCourseRegister(CourseRegister courseRegister) throws ServletException, IOException {
        courseRegisterService.editCourseRegister(courseRegister);
        autoCompleteService.initAutoComplete();
    }

    @RequestMapping(value = "delCourseRegister", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delCourseRegister(@RequestParam("id") Integer id) throws GeneralSecurityException {
        courseRegisterService.deleteCourseRegisterById(id);
        autoCompleteService.initAutoComplete();
    }


    @RequestMapping(value = "getSCOApiInfo", method = {RequestMethod.GET})
    @ResponseBody
    public ChapterRegister getSCOApiInfo(@RequestParam("id") Integer id) throws GeneralSecurityException {
        ChapterRegister chapterRegister = courseRegisterService.findCourseChapterRegisterById(id);
        return chapterRegister;
    }

    @RequestMapping(value = "commitScoApiInfo", method = {RequestMethod.POST})
    @ResponseBody
    public void commitScoApiInfo(ChapterRegister chapterRegister) throws GeneralSecurityException {
        courseRegisterService.editCourseChapterRegister(chapterRegister);
    }


    @RequestMapping(value = "selectEnterType", method = {RequestMethod.POST})
    @ResponseBody
    public void selectEnterType(ChapterRegister chapterRegisterInfo) throws GeneralSecurityException {
        ChapterRegister chapterRegister = courseRegisterService.findCourseChapterRegisterById(chapterRegisterInfo.getId());
        chapterRegister.setEntry(chapterRegisterInfo.getEntry());
        chapterRegister.setJudgeFinal(0);
        if (chapterRegister.getEntry().equals("ab-initio")) {
            chapterRegister.setLessonLocation("");
        }
        courseRegisterService.editCourseChapterRegister(chapterRegister);
    }

    @RequestMapping(value = "getLearnState", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public CourseRegister getLearnState(CourseRegister courseRegister) throws GeneralSecurityException {
        return courseRegisterService.findCourseRegisterByCourseIdAndUserId(courseRegister.getCourseId()).get(0);
    }

    @RequestMapping(value = "myRegister", method = {RequestMethod.POST, RequestMethod.GET})
    public String myRegister(HttpServletRequest request) throws GeneralSecurityException {
        Integer userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        request.setAttribute("typeValueses", classifyService.findAllTypeValue());
        request.setAttribute("courseRegisterList", courseRegisterService.findCourseRegisterByUserId(userId));
        return "frontstage/person/myRegisterDo";
    }

    @RequestMapping(value = "myCollect", method = {RequestMethod.POST, RequestMethod.GET})
    public String myCollect(HttpServletRequest request) throws GeneralSecurityException {
        Integer userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        request.setAttribute("typeValueses", classifyService.findAllTypeValue());
        request.setAttribute("courseCollectList", courseCollectService.findCourseCollectByUserId(userId));
        return "frontstage/person/myCollectDo";
    }

    @RequestMapping(value = "myUpload", method = {RequestMethod.POST, RequestMethod.GET})
    public String myUpload(HttpServletRequest request) throws GeneralSecurityException {
        Integer userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        List<Type> typeList = classifyService.findAllTypeList();
        List<TypeValues> typeValuesList = classifyService.findAllTypeValueList();
        request.setAttribute("typeList", typeList);
        request.setAttribute("typeValuesList", typeValuesList);
        request.setAttribute("typeValueses", classifyService.findAllTypeValue());
        request.setAttribute("courseUploadList", courseService.findUploadCourseListByUserId(userId));
        return "frontstage/person/myUploadDo";
    }
}
